package com.daydr3am.thenation;

import com.daydr3am.lib.CustomView;
import com.daydr3am.lib.TouchyWebView;
import com.eightbitcloud.pagingscroller.Pager;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;

public class RootScroll extends Pager implements OnTouchListener{
	public boolean interupt;
	boolean firstMessure=true;

    public RootScroll(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        this.setOnTouchListener(this);
        interupt=false;

    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        
        if(firstMessure){
	        View view=contents.getChildAt(0);
	        if(view==null)
	        	return;
	    	if(view.getClass().equals(CustomView.class)){
	    		((CustomView) view).onShowView();
	    	}
	    	else if(view.getClass().equals(TouchyWebView.class)){
	    		((TouchyWebView) view).onShowView();
	    		RootScroll main=(RootScroll)((Activity)con).findViewById(R.id.bigscroll);
	    	    main.interupt=true;
	    	}
	    	firstMessure=false;
        }
    }
    
    
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev){
		boolean event=super.onInterceptTouchEvent(ev);
        int width = getWidth();
		if(ev.getAction()==MotionEvent.ACTION_DOWN) 
			return false;
		
		if(ev.getAction()==MotionEvent.ACTION_UP)
			Log.v("hello", "intercept"+(interupt&event));
		
		return interupt&event; 
	}
	
    public void touchUpFinger(){
    	TheNationMain mainAc=(TheNationMain) getContext();
    	mainAc.scrollMain(getCurrentPage());
    	View view=contents.getChildAt(this.getCurrentPage());
    	Log.v("hello", "touch up view "+view.getClass()+" "+this.getCurrentPage());
    	if(view.getClass().equals(CustomView.class)){
    		((CustomView) view).onShowView();
    	}
    	else if(view.getClass().equals(TouchyWebView.class)){
    		((TouchyWebView) view).onShowView();
    		RootScroll main=(RootScroll)((Activity)con).findViewById(R.id.bigscroll);
    	    main.interupt=true;
    	}
    }
	
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}
}
